﻿using System;
using System.Web;

using NHibernate.Context;
using NHibernate;

namespace DflyingForum.Core.Data.NHHelper
{
    public class NHHttpModules : IHttpModule
    {
        #region IHttpModule 成员

        public void Dispose()
        {

        }

        public void Init(HttpApplication context)
        {
            context.BeginRequest += new EventHandler(context_BeginRequest);
            context.EndRequest += new EventHandler(context_EndRequest);
        }

        void context_BeginRequest(object sender, EventArgs e)
        {
            ISession Session = SessionManager.OpenSession();
            WebSessionContext.Bind(Session);
        }

        void context_EndRequest(object sender, EventArgs e)
        {
            if (WebSessionContext.HasBind(SessionManager.SessionFactory))
            {
                ISession Session = WebSessionContext.Unbind(SessionManager.SessionFactory);
                if (Session != null)
                {
                    Session.Close();
                    Session.Dispose();
                }
            }
        }

        #endregion
    }
}
